package com.java.onlineshop.service.impl;

import com.java.onlineshop.dao.IOrderStatusDao;
import com.java.onlineshop.pojo.OrderStatus;
import com.java.onlineshop.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @program: onlineshop
 * @description: IOrderStatusDao的实现类
 * @author: ShuangChenYue
 * @create: 2021-06-16 19:38
 **/
public class OrderStatusImpl implements IOrderStatusDao
{
    @Override
    public OrderStatus findById(int id)
    {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        OrderStatus orderStatus = null;
        try{
            //1.获取连接
            connection = JdbcUtil.getConnection();
            //1.5设置手动提交
            connection.setAutoCommit(false);
            //2.预编译sql语句
            String sql = "select * from orderstatus where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            //3.填充占位符
            preparedStatement.setInt(1,id);
            //4.执行操作并返回结果集
            resultSet = preparedStatement.executeQuery();
            //5.处理结果集
            while(resultSet.next())
            {
                //6.若结果集中有值，则保存进orderstatus的对象中
                orderStatus = new OrderStatus();
                orderStatus.setOrderstatusid(resultSet.getInt("orderstatusid"));
                orderStatus.setName(resultSet.getString("name"));
                orderStatus.setDescription(resultSet.getString("description"));
            }
            //6.提交
            connection.commit();
        }catch (Exception e)
        {
            //7.否则就数据回滚
            try
            {
                connection.rollback();
            } catch (SQLException throwables)
            {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally
        {
            //8.关闭资源
            JdbcUtil.relase(resultSet,preparedStatement,connection);
        }
        return null;
    }
}
